﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using BrainTechLLC.ThreadSafeObjects;
using System.Runtime.Serialization;
using System.IO;
using System.Xml.Serialization;

namespace BrainTechLLC
{
    
    /// <summary>
    /// Stores a username/login and password and info about which characters are owned, currently being played, etc
    /// </summary>
    [DataContract]
    [Serializable]
    public class UsernamePassword
    {
        [field: NonSerialized]
        public static event EventHandler<ObjectEventArgsNonRef<int, long>> WantSetCurrentlyPlaying;

        /// <summary>
        /// NOT registered via singleton class Everything - non-persistent Unique Id
        /// </summary>
        [DataMember]
        public long UniqueId { get; set; }

        [DataMember]
        public string UserName { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public ThreadSafeListValues<long> AssociatedIDs { get; set; }

        [DataMember]
        public long CurrentlyPlaying { get; set; }

        [NonSerialized]
        public int UniqueConnectionId;

        /// <summary>
        /// Standardized access to the ID/game character this login/connection is currently playing.  0 if none.
        /// </summary>
        public long IdIfCurrentlyPlaying
        {
            get
            {
                long id = CurrentlyPlaying;

                if (id > 0)
                    return id;

                return 0;
            }
        }

        public void OnWantSetCurrentlyPlaying(int uniqueId, long id)
        {
            if (WantSetCurrentlyPlaying != null)
                WantSetCurrentlyPlaying(this, new ObjectEventArgsNonRef<int, long>(uniqueId, id));
        }

        public void EnterGame()
        {
            UsernamesPasswords.OnEnterGame(this);
        }

        public void LeaveGame()
        {
            UsernamesPasswords.OnLeaveGame(this);
        }

        public void SetCurrentlyPlaying(int uniqueId, long id)
        {
            UniqueConnectionId = uniqueId;            
            CurrentlyPlaying = id;
            OnWantSetCurrentlyPlaying(uniqueId, id);
        }
    }
}
